package com.foreknow.demo7;

import java.util.Scanner;

public class EmployeeDAOImpl implements EmployeeDAO {
    private Employee[] employees;//声明一个数组容器
    int count;
    private Employee eid;

    public EmployeeDAOImpl(){
        //利用构造器初始化数组
        employees = new Employee[3];
    }

    /**
     * 添加
     * @param employee
     */
    @Override
    public void Add(Employee employee) {
        //1.判断参数employee是否为空，如果不为空
        //2.如果count<3
        //3.要将employee添加到数组容器中，count+1
        //4.输出信息
        if (employee!=null){
            if (count<3){
                employees[count] = employee;
                count++;
                System.out.println(employee.getName()+"的信息添加成功！");
            }else {
                System.out.println(employee.getName()+"的信息添加失败！");
            }
        }
    }

    /**
     * 查询全部信息
     * @return
     */
    @Override
    public Employee[] SelectAll() {
        //1.要对employees数组进行遍历，每遍历一次就要将原数组中获取到的员工
        //对象保存到新的数组中
        //2.返回新的数组
        Employee[] employees1 = new Employee[3];
        for(int i = 0;i<employees.length;i++){
            employees1[i] = employees[i];
        }
        return employees1;
    }

    /**
     * 修改
     * @param employee
     */
    @Override
    public void Update(Employee employee) {
        //1.可以先根据id查询要修改的对象
        //2.然后将查询到的对象传递给参数进行修改
        Scanner input = new Scanner(System.in);
        System.out.print("请输入要修改的员工编号:");
        int uid = input.nextInt();
        for (int i = 0;i<employees.length;i++) {
            if (uid == employees[i].getId()) {
                System.out.println("请输入修改后的员工姓名：");
                String uname = input.next();
                employee.setName(uname);
                System.out.println("请输入修改后的员工工资：");
                double usal = input.nextDouble();
                System.out.println("请输入修改后的员工部门：");
                String udept = input.next();
                employee.setId(uid);
                employee.setName(uname);
                employee.setSal(usal);
                employee.setDept(udept);
                employees[i] = employee;
                for (Employee emp:employees)
                    System.out.println(emp.getId()+"  "+emp.getName()+
                            "  "+emp.getSal()+"  "+emp.getDept());
            }
        }
    }

    /**
     * 通过id查询
     * @param id
     * @return
     */
    @Override
    public Employee SelectById(int id) {
        //1.对employees数组进行遍历
        //2.每遍历一次就要获取到一个Employee对象，判断Employee对象的id是否
        // 与参数中的id是否相等
        //3.最后将查询到的对象返回
        for (int i = 0;i<3;i++){
            eid = employees[i];
            if (eid.getId()==id){
                System.out.println("\n"+"编号为"+id+"的员工信息为："+
                        eid.getId()+"  "+eid.getName()+"  "+eid.getSal()+"  "+eid.getDept());
            }
        }
        return eid;
    }

    public static void main(String[] args) {
        EmployeeDAOImpl employees = new EmployeeDAOImpl();
        //Employee[] emp = employees.employees;
        Employee e1=new Employee(1001,"张三",13000,"宣传");
        Employee e2=new Employee(1002,"李四",11500,"人事");
        Employee e3=new Employee(1003,"王五",12200,"技术");
        employees.Add(e1);
        employees.Add(e2);
        employees.Add(e3);

        employees.SelectAll();
        for (Employee empl:employees.SelectAll())
            System.out.println(empl.getId()+"  "+empl.getName()+"  "+empl.getSal()+"  "+empl.getDept());

        employees.SelectById(1002);

        Employee emp = new Employee();
        employees.Update(emp);
    }
}
